﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using ThousandFunClient.model;
using ThousandFunClient.Util;
using System.Collections;
using System.IO.Ports;
using System.Timers;
using log4net;
using System.Globalization;
using System.Threading;
using System.Diagnostics ;
using System.IO;

namespace ThousandFunClient
{
    public partial class Fm_ImageChange : Form
    {
        public Fm_ImageChange()
        {
            InitializeComponent();

            DirectoryInfo di = new DirectoryInfo(imageconfigpath);

            
            foreach (FileInfo fi in di.GetFiles("*.jpg"))//可以用 di.GetFiles("*.jpg") 只移动jpg文件
            {
                filenum++;
                //执行重命名
                //e.
                //if(fi.FullName
                //    fi.MoveTo("新的路径");
            }

            images = new Image[filenum];

            for (int i = 0; i < filenum; i++)
            {
                images[i] = Image.FromFile(@imageconfigpath + i.ToString() + ".jpg");
               
            }

            pb_main.Image = images[runpicture];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Constants.TFEncryptkey

            //FileSystemWatcher
            string cc = CryptographyUtil.Encrypt("dd");
            MessageBox.Show(cc);

            string ff = CryptographyUtil.Decrypt(cc);
            MessageBox.Show(ff);
        }


        private Fm_About fm_about = new Fm_About();

        private static string watchpath = System.Configuration.ConfigurationSettings.AppSettings["watchpath"];
        private static string imageconfigpath = System.Configuration.ConfigurationSettings.AppSettings["imageconfigpath"];
        private static string removepathfinal = System.Configuration.ConfigurationSettings.AppSettings["removepath"];
        private static Image[] images ;// Image.FromFile(@"C:\\Documents and Settings\\All Users\Documents\\My Pictures\\示例图片\\Sunset.jpg");

        private static int runpicture = 0 ;
        private static int filenum = 0;
        // private static Image imagesec = Image.FromFile(@"C:\\Documents and Settings\\All Users\Documents\\My Pictures\\示例图片\\Water lilies.jpg");

        private void Fm_ImageChange_Load(object sender, EventArgs e)
        {
            // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = watchpath;
            /* Watch for changes in LastAccess and LastWrite times, and 
               the renaming of files or directories. */
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            // Only watch text files.
            watcher.Filter = "*.xml";

            // Add event handlers.
            //watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnChanged);
            //watcher.Deleted += new FileSystemEventHandler(OnChanged);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);

            // Begin watching.
            watcher.EnableRaisingEvents = true;
        }


        // Define the event handlers.
        private void OnChanged(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is changed, created, or deleted.
            //MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);

            File.Move(e.FullPath, removepathfinal + e.Name);
            runpicture++;
            if (runpicture >= filenum)
            {
                runpicture = 0;
            }
            pb_main.Image = images[runpicture];
            //Invoke(new MethodInvoker(delegate { pb_main.Image = images[runpicture]; }));

            //DirectoryInfo di = new DirectoryInfo(watchpath);

            //foreach (FileInfo fi in di.GetFiles())//可以用 di.GetFiles("*.jpg") 只移动jpg文件
            //{
            //    //执行重命名
            //    //e.
            //    //if(fi.FullName
            //    //    fi.MoveTo("新的路径");
            //}
            //fm_about.Show();
        }

        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            // Specify what is done when a file is renamed.
            MessageBox.Show( e.OldFullPath+"----"+e.FullPath);
        }


        private void button2_Click_1(object sender, EventArgs e)
        {
            fm_about.Show();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            runpicture = 0;
            pb_main.Image = images[runpicture];
        }

        private void pb_main_Click(object sender, EventArgs e)
        {

        }

 

    }
}
